//
//  FDMKindDetaileReusableView.m
//  Entertainment
//
//  Created by dllo on 16/4/25.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMKindDetaileReusableView.h"
#import "FDMFindDetaileModel.h"
#import "FDMKindHeaderDetailCollectionViewCell.h"

@interface FDMKindDetaileReusableView ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) FDMBaseCollectionView *collectionView;

@end
@implementation FDMKindDetaileReusableView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
    }
    return self;
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[FDMBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionView];
    [self.collectionView registerClass:[FDMKindHeaderDetailCollectionViewCell class] forCellWithReuseIdentifier:@"FDMKindHeaderDetailCollectionViewCell"];
    [self.collectionView registerClass:[FDMKindHeaderDetailCollectionViewCell class] forCellWithReuseIdentifier:@"FDMKindHeaderDetailCollectionViewCellSectionZero"];
    
}

#pragma mark - collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake((self.frame.size.width - 30) / 2, self.frame.size.height - 15);
    }
    return CGSizeMake((self.frame.size.width - 30) / 4, (self.frame.size.height - 20) / 2);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        FDMKindHeaderDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMKindHeaderDetailCollectionViewCell" forIndexPath:indexPath];
        FDMFindDetaileModel *model = self.modelArr[indexPath.row + 1];
        [cell.titleImage sd_setImageWithURL:[NSURL URLWithString:model.coverPath] placeholderImage:PLACEHOLDER_IMAGE];
        return cell;
    }
    FDMKindHeaderDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMKindHeaderDetailCollectionViewCellSectionZero" forIndexPath:indexPath];
    FDMFindDetaileModel *model = self.modelArr[indexPath.row];
    [cell.titleImage sd_setImageWithURL:[NSURL URLWithString:model.coverPath] placeholderImage:PLACEHOLDER_IMAGE];
    return cell;
}

- (void)setModelArr:(NSMutableArray *)modelArr {
    if (_modelArr != modelArr) {
        [_modelArr release];
        _modelArr = [modelArr retain];
        [self.collectionView reloadData];
    }
}

- (void)dealloc {
    [self.collectionView release];
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
